package com.meiyu.live.tv.widget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.eventbus.GiftPageUpdateEvent;
import com.meiyu.live.popj.GiftForLive;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 礼物分页Fragment
 */
public class GiftPageFragment extends BaseFragment {

    public static final String DATA_PAGE = "page";
    public static final int[] GIFT_COUNT_ARRAY = {1, 9, 99, 199, 520, 1314};

    private LayoutInflater inflater;
    private GridView gridView;
    private DataListAdapter adapter;
    private List<GiftForLive> giftList = new ArrayList<>();

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    /**
     * 页码
     */
    private int page = 0;

    public GiftPageFragment() {
    }

    public static GiftPageFragment newInstance(int page) {
        GiftPageFragment fragment = new GiftPageFragment();
        Bundle args = new Bundle();
        args.putInt(DATA_PAGE, page);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            page = getArguments().getInt(DATA_PAGE, 0);
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getContextView() {
        return R.layout.view_giftpager;
    }

    @Override
    protected void init() {
        inflater = LayoutInflater.from(getActivity());
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
        gridView = (GridView) findViewById(R.id.gridview);

        adapter = new DataListAdapter();
        gridView.setAdapter(adapter);
    }

    public void setData(List<GiftForLive> list) {
        if (list != null && list.size() > 0) {
            giftList.clear();
            giftList.addAll(list);
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }
    }

    class DataListAdapter extends BaseAdapter {

        private class ViewHolder {
            View layout_root;
            ImageView iv_gift;
            TextView tv_giftname, tv_giftcount, tv_giftprice;
        }

        View.OnClickListener onItemClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = (int) view.getTag();
                final GiftForLive bean = giftList.get(page * GiftSendView.COUNT_PER_PAGE + index);
                if (bean.isSelected) {
                    if (bean.getIs_double_click() == 1) {
                        bean.gift_count_index += 1;
                        if (bean.gift_count_index >= GIFT_COUNT_ARRAY.length) {
                            bean.gift_count_index = 0;
                        }
                        bean.gift_count = GIFT_COUNT_ARRAY[bean.gift_count_index];
                        EventBus.getDefault().post(new GiftPageUpdateEvent());
                    } else {
                        bean.gift_count = 0;
                        bean.isSelected = false;
                        EventBus.getDefault().post(new GiftPageUpdateEvent());
                    }
                } else {
                    for (GiftForLive giftForLive : giftList) {
                        giftForLive.gift_count = 0;
                        giftForLive.isSelected = false;
                    }
                    bean.gift_count_index = 0;
                    bean.gift_count = GIFT_COUNT_ARRAY[bean.gift_count_index];
                    bean.isSelected = true;
                    EventBus.getDefault().post(new GiftPageUpdateEvent());
                }
            }
        };

        @Override
        public int getCount() {
            return Math.min(GiftSendView.COUNT_PER_PAGE, giftList.size() - page * GiftSendView.COUNT_PER_PAGE);
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adapter_giftgrid, null);
                holder = new ViewHolder();
                holder.layout_root = convertView.findViewById(R.id.layout_root);
                holder.layout_root.setOnClickListener(onItemClickListener);
                holder.iv_gift = (ImageView) convertView.findViewById(R.id.iv_gift);
                holder.tv_giftname = (TextView) convertView.findViewById(R.id.tv_giftname);
                holder.tv_giftcount = (TextView) convertView.findViewById(R.id.tv_giftcount);
                holder.tv_giftprice = (TextView) convertView.findViewById(R.id.tv_giftprice);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.layout_root.setTag(position);
            final GiftForLive bean = giftList.get(page * GiftSendView.COUNT_PER_PAGE + position);
            imageLoader.displayImage(bean.getImage_url(), holder.iv_gift, options);
            holder.tv_giftname.setText(bean.getName());
            holder.tv_giftcount.setText(bean.gift_count > 0 ? bean.gift_count + "" : "");
            holder.layout_root.setBackgroundResource(bean.gift_count > 0 ? R.drawable.background_gift_selected : R.color.trans);
            holder.tv_giftprice.setText(bean.getUnit_price() + "钻石");
            return convertView;
        }
    }

    public void onEvent(GiftPageUpdateEvent event) {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
